package dao;

import java.util.List;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import model.Prod_ped;

public class Prod_pedDAO extends GenericDAO {
    
    Prod_ped prodPed = new Prod_ped();
    
    public Prod_ped findById(Long id){
        try{
            this.OpenConection();
            prodPed = this.em.find(Prod_ped.class, id);
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao consultar - "+e.getMessage());
        }finally{
            this.CloseConection();
        }
        return prodPed;
    }
    
//    public Prod_ped findByName(String nome){
//        try{
//            this.OpenConection();
//            Query consulta;
//            consulta = em.createQuery("select u from Prod_ped u where u.nome = :nome");
//            consulta.setParameter("nome", nome);
//            prodPed = (Preco)consulta.getSingleResult();
//            JOptionPane.showMessageDialog(null, lote.getNome());
//        }catch(Exception e){
//            JOptionPane.showMessageDialog(null, "Erro ao consultar - "+e.getMessage());
//        }finally{
//            this.CloseConection();
//        }
//        return prodPed;
//    }
    
    public void delete(Prod_ped a) {
        try {
            this.OpenConection();
            em.remove(em.getReference(Prod_ped.class, a.getId()));
            em.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Deletado com sucesso !");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro ao deletar - "+e.toString());
            em.getTransaction().rollback();
            System.out.println(e.getStackTrace());
            
        } finally {
            this.CloseConection();
        }
    }
    
    public List<Prod_ped> findAll(){
        try{
            this.OpenConection();
            Query consulta;
            consulta = em.createQuery("SELECT u FROM Prod_ped u", Prod_ped.class);
            List<Prod_ped> lista;
            lista = consulta.getResultList();
            return lista;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro ao listar - "+e.toString());
            em.getTransaction().rollback();
            System.out.println(e.getStackTrace());
            return null;
        }
    }
    
}//CLASS
